package com.molichuxing.gateway.bff.order.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.order.convertor.Convert;
import com.molichuxing.gateway.bff.order.entity.response.*;
import com.molichuxing.gateway.bff.order.services.OrderSubscribeBffService;
import com.molichuxing.services.business.dto.response.OrderSubscribeBizDto;
import com.molichuxing.services.business.dto.response.OrderUsedCarBizDto;
import com.molichuxing.services.business.service.OrderSubscribeBizService;
import com.molichuxing.services.business.service.OrderTradeFlowBizService;
import com.molichuxing.services.business.service.VehicleBizService;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.infrastructure.service.*;
import com.molichuxing.services.property.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 订阅里程订单相关bff
 * 
 * @author zoumingyu
 * @date 2019年8月13日
 *
 */
@Service("orderSubscribeBffService")
public class OrderSubscribeBffServiceImpl implements OrderSubscribeBffService {
	private static final Logger logger = LoggerFactory.getLogger(OrderSubscribeBffServiceImpl.class);

	@Resource
	private OrderSubscribeService orderSubscribeService;

	@Resource
	private OrderSubscribeBizService orderSubscribeBizService;

	@Resource
	private OrderReturnService orderReturnService;

	@Resource
	private OrderTradeFlowBizService orderTradeFlowBizService;

	@Resource
	private OrderMileageService orderMileageService;

	@Resource
	private OrderSubscribeSettlementService orderSubscribeSettlementService;

	@Resource
	private UserService userService;

	@Resource
	private StoreService storeService;

	@Resource
	private CarService carService;

	@Resource
	private VehicleBizService vehicleBizService;

	@Resource
	private PaymentService paymentService;

	@Resource
	private IovCarService iovCarService;

	/**
	 * 分页获取订阅里程订单信息
	 * 
	 * @param params
	 *            listType：列表类型-enum <br />
	 *            orderStatus：订单状态-enum <br />
	 *            orderCode：订单号-long <br />
	 *            userName：用户账号-string <br />
	 *            userRealName：用户姓名-string <br />
	 *            userPhone：用户手机号-string <br />
	 *            storeId：下单门店id-int <br />
	 *            orderTimeStart：下单开始时间-LocalDate <br />
	 *            orderTimeEnd：下单结束时间-LocalDate <br />
	 *            isHaveSuborder：有无续订-boolean
	 * @param dealerId
	 *            经销商id
	 * @param pageNum
	 *            页数
	 * @param pageSize
	 *            每页条数
	 * @return
	 */
	@Override
	public Paged<OrderSubscribeVo> getOrderList(Map<String, Object> params, Integer dealerId, int pageNum, int pageSize)
			throws Exception {
		Paged<OrderSubscribeVo> result = new Paged<>(new ArrayList<>(), 0, pageSize);
		params.put("source", 2);
		// 根据经销商id查询经销商下门店id
		List<Integer> dealerStoreIds = storeService.getAllStoreIdByDealer(dealerId);
		if (dealerStoreIds == null || dealerStoreIds.isEmpty()) {
			// 经销商下无门店信息，查询tab标签
			result.setTab(orderSubscribeService.getStatusStatistics(
					ObjectCastUtil.castEmun(params.get("listType"), OrderListTypeEnum.class), dealerStoreIds));
			return result;
		}

		// 判断查询条件是否有用户信息
		if (StringUtils.isNotBlank(ObjectCastUtil.castString(params.get("userName")))
				|| StringUtils.isNotBlank(ObjectCastUtil.castString(params.get("userRealName")))
				|| StringUtils.isNotBlank(ObjectCastUtil.castString(params.get("userPhone")))) {
			// 查询用信息
			List<UserDto> userDtoList = userService.getUserList(ObjectCastUtil.castString(params.get("userName")),
					ObjectCastUtil.castString(params.get("userRealName")),
					ObjectCastUtil.castString(params.get("userPhone")));
			if (userDtoList == null || userDtoList.isEmpty()) {
				// 用户不存在,查询tab标签
				result.setTab(orderSubscribeService.getStatusStatistics(
						ObjectCastUtil.castEmun(params.get("listType"), OrderListTypeEnum.class), dealerStoreIds));
				return result;
			}
			List<Integer> userIdList = new ArrayList<>();
			for (UserDto userDto : userDtoList) {
				userIdList.add(userDto.getId());
			}
			params.put("userIds", userIdList);
			params.remove("userName");
			params.remove("userRealName");
			params.remove("userPhone");
			logger.info("[getOrderList]分页获取订阅里程订单信息userIds：" + userIdList);
		}
		
		// 判断查询条件是否有门店信息
		Integer storeId = ObjectCastUtil.castInteger(params.get("storeId"));
		if (storeId != null) {
			// 判断该门店是否属于该经销商
			if (!dealerStoreIds.contains(storeId)) {
				// 不属于该经销商
				result.setTab(orderSubscribeService.getStatusStatistics(
						ObjectCastUtil.castEmun(params.get("listType"), OrderListTypeEnum.class), dealerStoreIds));
				return result;
			}
			params.put("storeIds", Arrays.asList(params.get("storeId")));
			params.remove("storeId");
		} else {
			params.put("storeIds", dealerStoreIds);
		}
		params.put("dealerStoreIds", dealerStoreIds);

		// 车架号、车牌号
		String vin = ObjectCastUtil.castString(params.get("vin"));
		String licencePlate = ObjectCastUtil.castString(params.get("licencePlate"));
		if (vin != null || licencePlate != null) {
			CarDto car = carService.getByVinLicencePlate(vin, licencePlate);
			if (car != null) {
				params.put("carId", car.getId());
			}
		}

		Paged<OrderSubscribeBizDto> orderSubscribeBizDtoPage = orderSubscribeBizService.getOrderList(params, pageNum,
				pageSize);
		// 判断是否查询为空
		if (orderSubscribeBizDtoPage == null) {
			result.setTab(orderSubscribeService.getStatusStatistics(
					ObjectCastUtil.castEmun(params.get("listType"), OrderListTypeEnum.class), dealerStoreIds));
			return result;
		} else if (orderSubscribeBizDtoPage.isEmpty()) {
			return result.setTotal(orderSubscribeBizDtoPage.getTotal()).setTab(orderSubscribeBizDtoPage.getTab());
		}

		result.setTotal(orderSubscribeBizDtoPage.getTotal()).setTab(orderSubscribeBizDtoPage.getTab());

		// 用户id集合
		List<Integer> userIds = new ArrayList<>();
		// 门店id集合
		List<Integer> storeIds = new ArrayList<>();
		// 车型id集合
		List<Integer> vehicleIds = new ArrayList<>();
		// 订单号集合
		List<Long> orderCodes = new ArrayList<>();
		// 车辆id集合
		List<Integer> carIds = new ArrayList<>();
		for (OrderSubscribeBizDto orderSubscribeBizDto : orderSubscribeBizDtoPage.getList()) {
			userIds.add(orderSubscribeBizDto.getUserId());
			storeIds.add(orderSubscribeBizDto.getPlaceStoreId());
			vehicleIds.add(orderSubscribeBizDto.getVehicleId());
			orderCodes.add(orderSubscribeBizDto.getOrderCode());
			carIds.add(orderSubscribeBizDto.getCarId());
			result.getList().add(Convert.toOrderSubscribeVo(orderSubscribeBizDto));
		}
		// 查询用户
		Map<Integer, UserDto> userMap = userService.getUsers(userIds);
		// 查询门店
		Map<Integer, StoreDto> storeMap = storeService.getStoreMapByIds(storeIds);
		// 查询车型
		Map<Integer, String> vehicleMap = vehicleBizService.getVehicleFullName(vehicleIds);
		// 查询车辆
		Map<Integer, CarDto> carMap = null;
		if (!carIds.isEmpty()) {
			carMap = carService.getCarByIds(carIds);
		}
		if ((userMap == null || userMap.isEmpty()) && (storeMap == null || storeMap.isEmpty())
				&& (vehicleMap == null || vehicleMap.isEmpty()) && (carMap == null || carMap.isEmpty())) {
			return result;
		}
		// 查询还车
		Map<Long, OrderReturnDto> ordeRedurnMap = null;
		if (OrderListTypeEnum.RETURN_LIST
				.equals(ObjectCastUtil.castEmun(params.get("listType"), OrderListTypeEnum.class))) {
			ordeRedurnMap = orderReturnService.getMapByOrderCodes(orderCodes);
		}
		for (OrderSubscribeVo orderSubscribeVo : result.getList()) {
			// 用户
			UserDto userDto = userMap == null ? null
					: userMap.isEmpty() ? null : userMap.get(orderSubscribeVo.getUserId());
			// 用户账号
			orderSubscribeVo.setUserName(userDto == null ? null : userDto.getName());
			// 用户手机号
			orderSubscribeVo.setUserPhone(userDto == null ? null : userDto.getPhone());
			// 用户姓名
			orderSubscribeVo.setUserRealName(userDto == null ? null : userDto.getRealName());
			// 门店
			StoreDto storeDto = storeMap == null ? null
					: storeMap.isEmpty() ? null : storeMap.get(orderSubscribeVo.getPlaceStoreId());
			// 下单门店(简称)
			orderSubscribeVo.setPlaceStoreName(storeDto == null ? null : storeDto.getShortName());
			// 车型名称
			orderSubscribeVo
					.setVehicleName(vehicleMap == null ? null : vehicleMap.get(orderSubscribeVo.getVehicleId()));

			// 还车dto
			OrderReturnDto orderReturnDto = ordeRedurnMap == null ? null
					: ordeRedurnMap.isEmpty() ? null : ordeRedurnMap.get(orderSubscribeVo.getOrderCode());
			// 还车时间
			orderSubscribeVo.setReturnTime(orderReturnDto == null ? null : orderReturnDto.getReturnTime());

			// 车辆信息
			if (carMap == null || carMap.isEmpty()) {
				continue;
			}
			CarDto car = carMap.get(orderSubscribeVo.getCarId());
			// 车架号
			orderSubscribeVo.setVin(car == null ? null : car.getVin());
			// 车牌号
			orderSubscribeVo.setLicencePlate(car == null ? null : car.getLicencePlate());
		}

		return result;
	}

	/**
	 * 获取订阅里程订单交易流程
	 * 
	 * @param orderCode
	 *            订单号
	 * @param listType
	 *            列表类型-enum
	 * 
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrderTradeFlowVo> getOrderTradeFlow(Long orderCode, OrderListTypeEnum listType) throws Exception {
		return orderCode == null ? null
				: Convert.toOrderTradeFlowVo(orderTradeFlowBizService.getSubscribeOrderFlow(orderCode, listType));
	}

	/**
	 * 获取订单基础信息(包含续订单)
	 *
	 * @param orderCode 订单号
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrderSubscribeBaseVo> getOrderBaseList(Long orderCode, Boolean isSuborder,
	                                                   List<OrderSubscribeBaseVo> result, int num) throws Exception {

		num++;
		OrderSubscribeBizDto orderSubscribeBizDto = null;
		if (isSuborder) {
			// 查询续订单
			orderSubscribeBizDto = orderSubscribeBizService.getEffectiveOrderSubscribeByParent(orderCode);
		} else {
			orderSubscribeBizDto = orderSubscribeBizService.getOrderSubscribe(orderCode);
		}

		if (orderSubscribeBizDto == null) {
			return result;
		}
		if (result == null) {
			result = new ArrayList<>();
		}
		result.add(Convert.toOrderSubscribeBaseVo(orderSubscribeBizDto));

		if (orderSubscribeBizDto.getIsHaveSuborder() && num < 100) {
			// 存在子订单
			result = getOrderBaseList(orderSubscribeBizDto.getOrderCode(), true, result, num);
		} else {
			// 查询门店信息
			StoreDto storeDto = storeService.getStoreById(orderSubscribeBizDto.getPlaceStoreId());
			// 查询车型信息
			String vehicleName = vehicleBizService.getVehicleName(orderSubscribeBizDto.getVehicleId());
			// 查询车辆信息
			CarDto carDto = null;
			if (orderSubscribeBizDto.getCarId() != null) {
				carDto = carService.getCarById(orderSubscribeBizDto.getCarId());
			}

			for (OrderSubscribeBaseVo orderSubscribeBaseVo : result) {
				// 门店简称
				orderSubscribeBaseVo.setPlaceStoreName(storeDto.getShortName());
				// 车型名称
				orderSubscribeBaseVo.setVehicleName(vehicleName);
				// 车架号
				orderSubscribeBaseVo.setVin(carDto == null ? null : carDto.getVin());
				// 车牌号
				orderSubscribeBaseVo.setLicensePlate(carDto == null ? null : carDto.getLicencePlate());

				orderSubscribeBaseVo.setBusinessType(OrderTypeEnum.SUBSCRIBE.getValue());
				orderSubscribeBaseVo.setBusinessTypeStr(OrderTypeEnum.SUBSCRIBE.getName());
			}
		}

		return result;
	}

	/**
	 * 获取订单基础信息(包含该订单相关联的所有订单（主订单、续订单）)
	 *
	 * @param orderCode 订单号
	 * @param result    结果
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<OrderSubscribeBaseVo> getOrderBaseList(Long orderCode, List<OrderSubscribeBaseVo> result) throws Exception {
		// 查询订单信息
		OrderSubscribeBizDto orderSubscribeBizDto = orderSubscribeBizService.getOrderSubscribe(orderCode);

		// 判断是否存在主订单
		if (orderSubscribeBizDto.getParentOrderCode() != null) {
			// 存在，查询主订单信息（递归查询所有主订单）
			result = this.getParentOrderBaseList(orderSubscribeBizDto.getParentOrderCode(), result, 0);
		}

		result = this.getOrderBaseList(orderCode, false, result, 0);

		return result;
	}

	private List<OrderSubscribeBaseVo> getParentOrderBaseList(Long parentOrderCode, List<OrderSubscribeBaseVo> result,
	                                                          int num) {
		num++;
		if (result == null) {
			result = new ArrayList<>();
		}
		if (num > 100) {
			return result;
		}
		// 查询主订单信息
		OrderSubscribeBizDto orderSubscribeBizDto = orderSubscribeBizService.getOrderSubscribe(parentOrderCode);

		if (orderSubscribeBizDto == null) {
			return result;
		}

		result.add(0, Convert.toOrderSubscribeBaseVo(orderSubscribeBizDto));
		// 判断是否存在主订单
		if (orderSubscribeBizDto.getParentOrderCode() != null) {
			// 存在，查询主订单信息（递归查询所有主订单）
			result = this.getParentOrderBaseList(orderSubscribeBizDto.getParentOrderCode(), result, num);
		}

		return result;
	}


	/**
	 * 获取用车实况信息
	 *
	 * @param orderCode 订单号
	 * @return
	 * @throws Exception
	 */
	@Override
	public OrderUsedCarVo getOrderUsedCar(Long orderCode) throws Exception {
		OrderUsedCarVo result = null;
		OrderUsedCarBizDto orderUsedCarBizDto = orderSubscribeBizService.getOrderUsedCar(orderCode);
		if (orderUsedCarBizDto == null) {
			return result;
		}
		result = Convert.toOrderUsedCarVo(orderUsedCarBizDto);
		// 判断是否已经提车
		if (orderUsedCarBizDto.getCarId() == null) {
			// 未提车
			result.setCurrentMileage(null);
			result.setTravelMileage(null);
			result.setSurplusMileage(null);
			result.setBeyondMileage(null);
			return result;
		} else if (OrderSubscribeStatusEnum.TRADE_UNUSE.equals(orderUsedCarBizDto.getOrderStatus())) {
			// 待用车，不查询
			result.setCurrentMileage(null);
			result.setTravelMileage(null);
			result.setSurplusMileage(null);
			result.setBeyondMileage(null);
			return result;
		}
		// 查询车辆信息
		CarDto carDto = carService.getCarById(orderUsedCarBizDto.getCarId());
		// 车辆当前里程数
		if (OrderSubscribeStatusEnum.TRADE_USE.equals(orderUsedCarBizDto.getOrderStatus())) {
			// 用车中，调用车联网查询
			Integer currentMileage = iovCarService.getCurrentMileage(carDto.getVin());
			if (currentMileage == null) {
				throw new BizException("查询车辆里程异常");
			}
			result.setCurrentMileage(currentMileage);
			// 计算已行驶里程（当前里程-起始里程）
			result.setTravelMileage(result.getCurrentMileage() - result.getStartMileage());
			// 计算剩余里程(预警里程-当前里程)
			Integer surplusMileage = orderUsedCarBizDto.getWarningMileage() - result.getCurrentMileage();
			result.setSurplusMileage(surplusMileage >= 0 ? surplusMileage : 0);
			// 已超时长
			Integer day = (int) DateUtil.getDaysDiff(orderUsedCarBizDto.getLeaseExpire(), LocalDateTime.now());
			if (day > 0) {
				// 已超时长
				result.setBeyondTime(day);
			}
		} else {
			// 其他状态，查询结算（还车时）里程
			result.setCurrentMileage(orderUsedCarBizDto.getSettleMileage());
			// 计算已行驶里程（当前里程-起始里程）
			result.setTravelMileage(result.getCurrentMileage() - result.getStartMileage());
			// 剩余里程
			result.setSurplusMileage(0);
			// 查询结算信息
			OrderSubscribeSettlementDto orderSubscribeSettlementDto = orderSubscribeSettlementService
					.getOrderSubscribeSettlement(orderCode);
			if (orderSubscribeSettlementDto != null && orderSubscribeSettlementDto.getBeyondDay() != null
					&& orderSubscribeSettlementDto.getBeyondDay() > 0) {
				// 已超时长
				result.setBeyondTime(orderSubscribeSettlementDto.getBeyondDay());
			} else {
				// 已超时长
				result.setBeyondTime(0);
			}
		}
		// 已超里程(已行驶里程-购买总里程+加购结算的超里程)
		Integer beyondMileage = result.getTravelMileage() - result.getTotalMileage();
		if (beyondMileage > 0) {
			result.setBeyondMileage(beyondMileage + result.getBeyondMileage());
		}

		return result;
	}

	/**
	 * 获取订单费用交易明细
	 * 
	 * @param orderCode
	 *            订单号
	 * @return
	 */
	@Override
	public List<OrderCostDetailsVo> getOrderCostDetails(Long orderCode) {
		List<OrderCostDetailsVo> result = null;
		OrderSubscribeBizDto orderSubscribeBizDto = orderSubscribeBizService.getOrderSubscribe(orderCode);
		if (orderSubscribeBizDto == null) {
			return result;
		}
		result = new ArrayList<>();
		String payWayStr = null;
		LocalDateTime paySuccessTime = null;
		Integer payStatus = PaymentStatusEnum.NO_PAYMENT.getValue();
		String payStatusStr = PaymentStatusEnum.NO_PAYMENT.getName();
		// 判断该订单是否已支付
		if (!OrderSubscribeStatusEnum.TRADE_UNPAID.equals(orderSubscribeBizDto.getOrderStatus())
				&& !OrderSubscribeStatusEnum.TRADE_CANCELED.equals(orderSubscribeBizDto.getOrderStatus())) {
			// 不是待支付和已取消状态，代表已支付，查询支付信息
			int cost = orderSubscribeBizDto.getParentOrderCode() == null ? PaymentCostTypeEnum.SUBSCRIBE.getValue()
					: PaymentCostTypeEnum.RENEW.getValue();
			List<PaymentDto> paymentDtoList = paymentService.getListByOrderCode(orderCode,
					PaymentStatusEnum.SUCCESS_PAYMENT.getValue(), cost);
			if (paymentDtoList != null && !paymentDtoList.isEmpty()) {
				payWayStr = paymentDtoList.get(0).getPayWay().getName();
				paySuccessTime = paymentDtoList.get(0).getPayTime();
				payStatus = paymentDtoList.get(0).getPayStatus().getValue();
				payStatusStr = paymentDtoList.get(0).getPayStatus().getName();
			}
		}
		// 租车费
		OrderCostDetailsVo rental = new OrderCostDetailsVo();
		rental.setCostItem("租车费");
		rental.setServiceContent("用车时长：" + orderSubscribeBizDto.getTransportTime() + "个月");
		rental.setAmount(orderSubscribeBizDto.getRental());
		rental.setPayWayStr(payWayStr);
		rental.setPayStatus(payStatus);
		rental.setPayStatusStr(payStatusStr);
		rental.setPaySuccessTime(paySuccessTime);
		result.add(rental);
		// 保证金
		if (orderSubscribeBizDto.getDeposit() != null
				&& orderSubscribeBizDto.getDeposit().compareTo(BigDecimal.ZERO) > 0) {
			OrderCostDetailsVo deposit = new OrderCostDetailsVo();
			deposit.setCostItem("保证金");
			deposit.setServiceContent("违章违约保证金");
			deposit.setAmount(orderSubscribeBizDto.getDeposit());
			deposit.setPayWayStr(payWayStr);
			deposit.setPayStatus(payStatus);
			deposit.setPayStatusStr(payStatusStr);
			deposit.setPaySuccessTime(paySuccessTime);
			result.add(deposit);
		}
		// 基础里程费
		OrderCostDetailsVo baseMileageFee = new OrderCostDetailsVo();
		baseMileageFee.setCostItem("基础里程费");
		baseMileageFee.setServiceContent("基础里程：" + orderSubscribeBizDto.getBaseMileage() + "公里");
		baseMileageFee.setAmount(orderSubscribeBizDto.getBaseMileagePrice());
		baseMileageFee.setPayWayStr(payWayStr);
		baseMileageFee.setPayStatus(payStatus);
		baseMileageFee.setPayStatusStr(payStatusStr);
		baseMileageFee.setPaySuccessTime(paySuccessTime);
		result.add(baseMileageFee);
		// 订阅里程费
		OrderCostDetailsVo packageMileage = new OrderCostDetailsVo();
		packageMileage.setCostItem("订阅里程费");
		packageMileage.setServiceContent("订阅里程：" + orderSubscribeBizDto.getPackageMileage() + "公里");
		packageMileage.setAmount(orderSubscribeBizDto.getPackageMileagePrice());
		packageMileage.setPayWayStr(payWayStr);
		packageMileage.setPayStatus(payStatus);
		packageMileage.setPayStatusStr(payStatusStr);
		packageMileage.setPaySuccessTime(paySuccessTime);
		result.add(packageMileage);

		// 判断是否存在加购
		if (orderSubscribeBizDto.getPurchaseMileage() != null && orderSubscribeBizDto.getPurchaseMileage() > 0) {
			// 存在，查询加购费用信息
			List<OrderMileageDto> orderMileageDtoList = orderMileageService
					.getOrderMileageList(orderCode, OrderMileagePayStatusEnum.PREPAID);

			if (orderMileageDtoList != null && !orderMileageDtoList.isEmpty()) {
				for (OrderMileageDto orderMileageDto : orderMileageDtoList) {
					List<PaymentDto> paymentDtoList = paymentService.getListByOrderCode(orderMileageDto.getOrderCode(),
							PaymentStatusEnum.SUCCESS_PAYMENT.getValue(), PaymentCostTypeEnum.PURCHASE.getValue());

					if (paymentDtoList != null && !paymentDtoList.isEmpty()) {
						// 加购费用
						OrderCostDetailsVo relet = new OrderCostDetailsVo();
						relet.setCostItem("订阅里程费");
						relet.setServiceContent("加购里程：" + orderMileageDto.getPurchaseMileage() + "公里");
						relet.setAmount(orderMileageDto.getPurchaseFee());
						relet.setPayWayStr(paymentDtoList.get(0).getPayWay().getName());
						relet.setPayStatus(paymentDtoList.get(0).getPayStatus().getValue());
						relet.setPayStatusStr(paymentDtoList.get(0).getPayStatus().getName());
						relet.setPaySuccessTime(paymentDtoList.get(0).getPayTime());
						result.add(relet);
						// 加购超里程费用
						if (orderMileageDto.getBeyondMileage() != null && orderMileageDto.getBeyondMileage() > 0) {
							OrderCostDetailsVo beyondMileage = new OrderCostDetailsVo();
							beyondMileage.setCostItem("超里程费用");
							beyondMileage.setServiceContent("超出里程：" + orderMileageDto.getBeyondMileage() + "公里");
							beyondMileage.setAmount(orderMileageDto.getBeyondFee());
							beyondMileage.setPayWayStr(paymentDtoList.get(0).getPayWay().getName());
							beyondMileage.setPayStatus(paymentDtoList.get(0).getPayStatus().getValue());
							beyondMileage.setPayStatusStr(paymentDtoList.get(0).getPayStatus().getName());
							beyondMileage.setPaySuccessTime(paymentDtoList.get(0).getPayTime());
							result.add(beyondMileage);
						}
					}
				}
			}
		}
		// 判断是否已完成结算
		if (OrderSubscribeStatusEnum.RETURN_FINISH.equals(orderSubscribeBizDto.getOrderStatus())) {
			// 查询结算信息
			OrderSubscribeSettlementDto orderSubscribeSettlementDto = orderSubscribeSettlementService
					.getOrderSubscribeSettlement(orderCode, OrderSubscribeSettlementPayStatusEnum.PREPAID);

			if (orderSubscribeSettlementDto != null) {
				// 查询结算支付信息
				List<PaymentDto> paymentDtoList = paymentService.getListByOrderCode(orderCode,
						PaymentStatusEnum.SUCCESS_PAYMENT.getValue(), PaymentCostTypeEnum.SETTLEMENT.getValue());
				if (paymentDtoList != null && !paymentDtoList.isEmpty()) {
					if (orderSubscribeSettlementDto.getBeyondMileageFee() != null
							&& orderSubscribeSettlementDto.getBeyondMileageFee().compareTo(BigDecimal.ZERO) > 0) {
						// 结算超里程费用
						OrderCostDetailsVo beyondMileage = new OrderCostDetailsVo();
						beyondMileage.setCostItem("超里程费用");
						beyondMileage
								.setServiceContent("超出里程：" + orderSubscribeSettlementDto.getBeyondMileage() + "公里");
						beyondMileage.setAmount(orderSubscribeSettlementDto.getBeyondMileageFee());
						beyondMileage.setPayWayStr(paymentDtoList.get(0).getPayWay().getName());
						beyondMileage.setPayStatus(paymentDtoList.get(0).getPayStatus().getValue());
						beyondMileage.setPayStatusStr(paymentDtoList.get(0).getPayStatus().getName());
						beyondMileage.setPaySuccessTime(paymentDtoList.get(0).getPayTime());
						result.add(beyondMileage);
					}
					if (orderSubscribeSettlementDto.getBeyondDayFee() != null
							&& orderSubscribeSettlementDto.getBeyondDayFee().compareTo(BigDecimal.ZERO) > 0) {
						// 结算超时费用
						OrderCostDetailsVo beyondDay = new OrderCostDetailsVo();
						beyondDay.setCostItem("超时费用");
						beyondDay.setServiceContent("超时：" + orderSubscribeSettlementDto.getBeyondDay() + "天");
						beyondDay.setAmount(orderSubscribeSettlementDto.getBeyondDayFee());
						beyondDay.setPayWayStr(paymentDtoList.get(0).getPayWay().getName());
						beyondDay.setPayStatus(paymentDtoList.get(0).getPayStatus().getValue());
						beyondDay.setPayStatusStr(paymentDtoList.get(0).getPayStatus().getName());
						beyondDay.setPaySuccessTime(paymentDtoList.get(0).getPayTime());
						result.add(beyondDay);
					}
					if (orderSubscribeSettlementDto.getDamageFee() != null
							&& orderSubscribeSettlementDto.getDamageFee().compareTo(BigDecimal.ZERO) > 0) {
						// 结算质损费用
						OrderCostDetailsVo damage = new OrderCostDetailsVo();
						damage.setCostItem("质损费用");
						damage.setServiceContent("车辆质损");
						damage.setAmount(orderSubscribeSettlementDto.getDamageFee());
						damage.setPayWayStr(paymentDtoList.get(0).getPayWay().getName());
						damage.setPayStatus(paymentDtoList.get(0).getPayStatus().getValue());
						damage.setPayStatusStr(paymentDtoList.get(0).getPayStatus().getName());
						damage.setPaySuccessTime(paymentDtoList.get(0).getPayTime());
						result.add(damage);
					}
				}
			}
		}

		return result;
	}
}
